﻿
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using System.Data;
using Newtonsoft.Json.Linq;

namespace Test_API
{
    class DiscussionForumTest : GenericModuleTest
    {
        protected override string moduleName
        {
            get
            {
                return "Discussion Forum Test";
            }
        }

        private string addDiscussionThreadMethod = "addDiscussionThread";
        private string addDiscussionThreadReplyMethod = "addDiscussionThreadReply";
        private string updateDiscussionThreadMethod = "updateDiscussionThread";
        private string updateDiscussionThreadReplyMethod = "updateDiscussionThreadReply";
        private List<int> validThreadIds;
        private List<int> validReplies;

        public DiscussionForumTest(string accessToken, string cid)
            : base(accessToken)
        {
            this.viewAllMethod = "viewAllDiscussionItems";
            this.viewSingleItemMethod = "viewDiscussionItem";
            this.deleteMethod = "deleteDiscussionItem";
            this.cid = cid;

            this.validThreadIds = new List<int>();
            this.validReplies = new List<int>();
        }

        protected override string genericQueryStringGenerate(int validItemId)
        {
            string queryString = new Dictionary<string, string>() { { "cid", this.cid }, { "accesstoken", this.accesstoken }, { "selfId", validItemId.ToString() } }.buildQueryString();

            return queryString;
        }
        protected override Dictionary<string, object> genericPostDataSetGenerate()
        {
            Dictionary<string, object> dataSet = new Dictionary<string, object>();
            {
                dataSet.Add("body", Library.getDescription());
                dataSet.Add("subject", Library.getTitle());
            }
            return dataSet;
        }

        protected List<LogEntry> viewAll()
        {
            List<LogEntry> entries = base.viewAll("selfId");
            try
            {
                JObject receivedJson = JObject.Parse(entries[0].receivedData);

                for (int i = 0; i < receivedJson["dataSet"].Count(); ++i)
                {
                    var selfId = receivedJson["dataSet"][i]["selfId"];
                    var replyTo = receivedJson["dataSet"][i]["replyToId"];
                    if ((string)replyTo == "0")
                    {
                        this.validThreadIds.Add(int.Parse(selfId.ToString()));
                    }
                    else
                        this.validReplies.Add(int.Parse(selfId.ToString()));
                    this.validItemIDs.Add(int.Parse(selfId.ToString()));
                }
            }
            catch (Exception e)
            {
                entries[0].verdict = false;
                entries[0].receivedData = (e is JsonException ? API_VERDICT.PARSING_FAILURE : String.Empty) + e.Message;
            }
            return entries;
        }

        private List<LogEntry> addOrUpdate(bool doAddOperation, bool goForThread)
        {
            List<LogEntry> entries = new List<LogEntry>();
            string methodUrl, queryString;

            LogEntry t1 = new LogEntry(this.moduleName, true);
            if (doAddOperation == true)
            {
                queryString = this.genericQueryStringGenerate(this.validItemIDs.randomElement());
                if (goForThread)
                {
                    methodUrl = this.addDiscussionThreadMethod;
                }
                else
                {
                    methodUrl = this.addDiscussionThreadReplyMethod;
                    queryString += "replyToId=" + this.validThreadIds.randomElement().ToString();
                }
            }
            else
            {
                methodUrl = goForThread ? this.updateDiscussionThreadMethod : this.updateDiscussionThreadReplyMethod;
                queryString = this.genericQueryStringGenerate(this.validItemIDs.randomElement());
            }
            HTTP.execute(HTTP.RequestType.POST, this.rootUrl, methodUrl, queryString, this.genericPostDataSetGenerate(), ref t1);
            t1.verdict &= API_VERDICT.checkAddOrUpdateOperation(t1.receivedData, doAddOperation);
            entries.Add(t1);
            return entries;
        }


        public new List<LogEntry> start()
        {
            Console.WriteLine("DiscussionForum API call begin\n============");
            List<LogEntry> entries = new List<LogEntry>();

            entries.AddRange(this.addOrUpdate(true, true)); //Add + Discussion
            entries.AddRange(this.viewAll());
            entries.AddRange(this.viewSingle());
            entries.AddRange(this.addOrUpdate(true, false)); //Add + Reply
            entries.AddRange(this.addOrUpdate(false, true)); //Update + Thread
            entries.AddRange(this.addOrUpdate(false, false)); //Update + Reply
            entries.AddRange(this.delete());
            return entries;
        }
    }
}